package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.CompanyInfoMapper;
import com.suyuan.dao.PigInMapper;
import com.suyuan.dao.ProInspectInfoMapper;
import com.suyuan.dao.PurchaseInfoMapper;
import com.suyuan.entity.*;
import com.suyuan.service.ProInspectService;
import com.suyuan.utils.DeleteRows;
import com.suyuan.vo.ProInspectInfoVO;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 检验检疫业务层
 */
@Service("proInspectService")
public class ProInspectServiceImpl implements ProInspectService {

    @Resource
    private ProInspectInfoMapper proInspectInfoMapper;

    @Resource
    private PigInMapper pigInMapper;

    @Resource
    private PurchaseInfoMapper purchaseInfoMapper;

    @Resource
    private CompanyInfoMapper companyInfoMapper;


    /**
     * 检验检疫异步查询生猪是否入场
     * @param pid
     * @return
     */
    public Boolean checkPurcharse(Integer pid) {
        PigInExample pigInExample = new PigInExample();
        PigInExample.Criteria criteria = pigInExample.createCriteria();
        criteria.andPurGidEqualTo(pid);
        List<PigIn> pigIn = pigInMapper.selectByExample(pigInExample);
        if(pigIn.size() > 0){
            return true;
        }
        return false;
    }

    /**
     *检验检疫信息添加
     * @param proInspectInfoVO
     * @return
     */
    public Boolean addProInspect(ProInspectInfoVO proInspectInfoVO) {
        ProInspectInfo proInspectInfo = proInspectInfoVO.getProInspectInfo();
        proInspectInfoMapper.insertSelective(proInspectInfo);
        if(proInspectInfoVO.getSlaughterPermitNo() != null){
            PurchaseInfo purchaseInfo =purchaseInfoMapper.selectByPrimaryKey(proInspectInfo.getPurcid());
            purchaseInfo.setProclineid(proInspectInfo.getProclineid());
            purchaseInfo.setProclinename(proInspectInfo.getProclinename());
            purchaseInfo.setSlaughterpermitno(proInspectInfoVO.getSlaughterPermitNo());
            purchaseInfoMapper.updateByPrimaryKeySelective(purchaseInfo);
        }
        return true;
    }


    /**
     * 根据生猪ID查询生猪检验检疫情况
     * @param purGid
     * @return
     */
    public PageInfo findByPurGid(Integer purGid,Page page) {
        ProInspectInfoExample proInspectInfoExample = new ProInspectInfoExample();
        ProInspectInfoExample.Criteria criteria = proInspectInfoExample.createCriteria();
        if(purGid != null){
            criteria.andPurcidEqualTo(purGid);
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<ProInspectInfo> proInspectInfoList = proInspectInfoMapper.selectByExample(proInspectInfoExample);
        PageInfo pageInfo = new PageInfo(proInspectInfoList);
        List<ProInspectInfoVO> proInspectInfoVOList = new ArrayList<ProInspectInfoVO>();
        for (ProInspectInfo proInspectInfo: (List<ProInspectInfo>) pageInfo.getList()){
            CompanyInfo companyInfo = companyInfoMapper.selectByPrimaryKey(proInspectInfo.getCompid());
            proInspectInfoVOList.add(new ProInspectInfoVO(proInspectInfo,companyInfo));
        }
        pageInfo.setList(proInspectInfoVOList);
        return pageInfo;
    }


    /**
     * 删除生猪信息
     * @param ids
     * @return
     */
    public Integer deletePigProInspectInInfo(String ids) {
        DeleteRows deleteRows = new DeleteRows();
        int [] deleteID = deleteRows.selectMore(ids);
        for (Integer ID : deleteID){
            proInspectInfoMapper.deleteByPrimaryKey(ID);
        }
        return 1;
    }

    /**
     * 单个删除生猪信息
     * @param id
     * @return
     */
    public int deleteInfo(Integer id) {
        proInspectInfoMapper.deleteByPrimaryKey(id);
        return 1;
    }

    /**
     * 更具检验检疫信息ID查询检验检疫所有信息，供编辑页面使用
     * @param gid
     * @return
     */
    public ProInspectInfo findByGid(Integer gid) {
        return proInspectInfoMapper.selectByPrimaryKey(gid);
    }

    /**
     * 更新页面信息
     * @param proInspectInfo
     */
    public void updateInfo(ProInspectInfo proInspectInfo) {
        ProInspectInfo proInspectInfo1 = proInspectInfoMapper.selectByPrimaryKey(proInspectInfo.getGid());
        //更新生猪屠宰信息
        PurchaseInfo purchaseInfo =purchaseInfoMapper.selectByPrimaryKey(proInspectInfo.getPurcid());
        purchaseInfo.setProclineid(proInspectInfo.getProclineid());
        purchaseInfo.setProclinename(proInspectInfo.getProclinename());
        purchaseInfo.setSlaughterpermitno(proInspectInfo.getReserve1());
        purchaseInfoMapper.updateByPrimaryKeySelective(purchaseInfo);

        //跟新检疫检验信息 备用字段reserve1为屠宰许可证编号，纯粹为编码方便
        proInspectInfo1.setInspectorid(proInspectInfo.getInspectorid());
        proInspectInfo1.setProclineid(proInspectInfo.getProclineid());
        proInspectInfo1.setProclinename(proInspectInfo.getProclinename());
        proInspectInfo1.setInspectorname(proInspectInfo.getInspectorname());
        proInspectInfo1.setInspecttime(proInspectInfo.getInspecttime());
        proInspectInfo1.setInspectype(proInspectInfo.getInspectype());
        proInspectInfo1.setReserve1(proInspectInfo.getReserve1());
        proInspectInfo1.setResultnotes(proInspectInfo.getResultnotes());
        proInspectInfo1.setResulttype(proInspectInfo.getResulttype());
        proInspectInfoMapper.updateByPrimaryKeySelective(proInspectInfo1);
    }


}
